package com.archgeek.rpc.thrfit;

import com.archgeek.rpc.thrfit.gen.StudentService;
import org.junit.Test;

/**
 * @author pizhihui
 * @date 2022-04-07 13:00
 */

public class ThriftClientTest {

    private SocketPool pool = new SocketPool("127.0.0.1", 8732);

    private volatile boolean flag = false; // 用于保证有一个线程已经开启

    @Test
    public void testOne() throws InterruptedException {

        int jobCount = 200;
        for (int i = 0; i < jobCount; i++) {
            new Job().start();
        }

        System.out.println("启动完了所有任务..........");

        monitorPool();

    }

    private void monitorPool() {
        while (true) {
            boolean tem = flag; // 重要，跳出循环不能直接使用flag;
            int active = pool.getNumActiveClient();
            int idle = pool.getNumIdleClient();
            int maxPoolSize = pool.getMaxPoolSize();

//            pool.setMaxPoolSize(500);

            System.out.println("active=" + active + ", idle=" + idle + ", maxTotal=" + maxPoolSize);
            if (tem && active == 0 && idle == pool.getMinIdle()) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    class Job extends Thread {

        @Override
        public void run() {
            try {
                StudentService.Client client = pool.getClicent();

                System.out.println("active=" + pool.getNumActiveClient() + ", idle=" + pool.getNumIdleClient() + ", maxTotal=" + pool.getMaxPoolSize());

                String s = client.sayHello("123");
                System.out.println(s);

                pool.releaseClient(client);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




}
